<?php
namespace app\admin\controller;

class Combank extends Base
{

    //列表
    public function lists(){
      if(is_post()){
          $d = $this->post;
          $map = [];
          if($d['keyword']) $map[] = ['and',"bank_name","like","%{$d['keyword']}%"];
          if($d['min_time']) $map[] = ['and','create_time','>=',strtotime($d['min_time'])];
          if($d['max_time']) $map[] = ['and','create_time','<=',strtotime($d['max_time'])];
          $w  = where($map);
          $sql = "select * from com_bank  $w[0] order by id desc".limit($d['page'],$d['limit']);
          $rs = $this->db->query($sql, $w[1],'',true);
          if($rs){
              $rule = [
                  'create_time' => 'com_time_format',
              ];
              $data = com_format($rs[0],$rule);
              return suc($data, $rs[1]);
          }else{
              suc([],0);
          }
      }
      $this->fetch();
    }

    //添加
    public function add()
    {
        if (is_post()) {
            $d = $this->post;
            $rule = [
                'bank_name' => 'require',
            ];
            $msg = [
                'bank_name.require' => '请填写银行名称',
            ];
            check($rule, $msg, $d);
            $background_pic = "";
            if(isset($d["pics"])){
                $background_pic = $d["pics"][0];
            }
            $data = [
                "bank_name" => $d["bank_name"],
                "pic" => $d["pic"],
                "background_pic" => $background_pic,
                "create_time" => time(),
            ];
            $rs = $this->db->insert("com_bank",$data);
            if ($rs) {
                cookie('js_msg', '添加成功');
                cookie('js_function', 'preload');
                return suc();
            } else {
                err('添加失败');
            }
        }
        return $this->fetch();
    }

    //编辑
    public function edit()
    {
        if (is_post()) {
            $d = $this->post;
            $rule = [
                'bank_name' => 'require',
            ];
            $msg = [
                'bank_name.require' => '请填写银行名称',
            ];
            check($rule, $msg, $d);
            $background_pic = "";
            if($d["pics"]){
                $background_pic = $d["pics"][0];
            }
            $data = [
                "id" => $d["id"],
                "bank_name" => $d["bank_name"],
                "pic" => $d["pic"],
                "background_pic" => $background_pic,
                "create_time" => time(),
            ];
            $rs = $this->db->update('com_bank', $data);
            if ($rs) {
                cookie('js_msg', '修改成功');
                cookie('js_function', 'preload');
                return suc();
            } else {
                err('修改失败');
            }
        }
        $id = $this->get['id'];
        $info = $this->db->query("select * from com_bank where id=?", [$id], 1);
        return $this->fetch(['info' => $info]);
    }

    //删除
    public function del()
    {
        if (is_post()) {
            $d = $this->post;
            $this->db->delete('com_bank', $d['id']);
            cookie('js_msg', '删除成功');
            cookie('js_function', 'reload');
            return suc();
        }
    }

    #批量删除
    function delall()
    {
        if (is_post()) {
            $d = $this->post;
            $ids = $d['id'];
            foreach ($ids as $id) {
                $this->db->delete('com_bank', $id);
            }
            cookie('js_msg', '删除成功');
            cookie('js_function', 'reload');
            return suc();
        }
    }
}
